package com.eboss2.sbs.activity;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.myapplication.MyApplication;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.utils.DataCleanManager;
import com.mytools.tool.utils.SystemUtil;
import com.mytools.tool.views.MyDialog;
import com.tool.utils.utils.LogUtils;
import com.tool.utils.utils.QRCodeUtil;
import com.tool.utils.view.ToggleButton;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;

public class MySettingActivity extends BaseActivity implements View.OnClickListener{

	private static final String TAG = "MySettingActivity";
	private double currentCacheSize = 0.0;

	private ToggleButton toggleButton1;
	private ToggleButton toggleButton2;
	private ToggleButton toggleButton3;
	private ToggleButton toggleButton4;

	private TextView logout;


	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_my_setting);
		MyActivityManager.getInstance().addActivity(this);
		initView();
	}

	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "设置");




		Bitmap bitmap = QRCodeUtil.createQRCodeBitmap("https://app.ebank007.com/androidphone/eboss2/eboss2.apk", 400);

		imageView(R.id.img_qrcode).setImageBitmap(bitmap);

		
		currentCacheSize = DataCleanManager.getCacheSize(mContext);
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		
		setTvText(R.id.bottom, "当前版本号："+SystemUtil.getVerName(mContext));

		relativeLayout(R.id.rl_clear_cache).setOnClickListener(this);
		relativeLayout(R.id.rl_about_sbs).setOnClickListener(this);
		linearLayout(R.id.logout).setOnClickListener(this);




	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			case R.id.logout:
				doCommit();

				break;
			case R.id.rl_about_sbs:

				break;
			case R.id.rl_clear_cache:
				MyDialog.Builder builder = new MyDialog.Builder(this);
				builder.setTitle("提示");
				builder.setMessage("确定要清除缓存，请谨慎操作！");
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						cleanCacheData();
					}
				});
				builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						dialog.dismiss();
					}
				});
				builder.create().show();
				break;
			default: break;
		}
	}

	private void doCommit() {
		String url = Config.SBS_LOGOUT;
		RequestParams params = new RequestParams();
		params.put("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid()+"");
		showProgress("加载中...");

		HttpUtil.get(url, params, new HttpUtil.RequestListener() {

			@Override
			public void success(String response) {
				disShowProgress();
				try {

					JSONObject result = new JSONObject(response);
					if (!result.optBoolean("status")) {
						showErrorMsg(result.getString("message"));
						return;
					}
					MyApplication.getmInstance().setNull();
					MyActivityManager.getInstance().logout(mContext);
					PushServiceFactory.getCloudPushService().unbindAccount(new CommonCallback() {
						@Override
						public void onSuccess(String s) {
							LogUtils.e(TAG, s);
//							ToastUtils.showShort(mContext, s);
						}

						@Override
						public void onFailed(String s, String s1) {

						}
					});
//					JSONObject obj = result.getJSONObject("data");
//					String mallGoodOutDTOList = obj.optString("mallGoodOutDTOList");
//					if (!StringUtils.isBlank(mallGoodOutDTOList)) {
//
//						Gson gson = new Gson();
//						// json数据转换成List
//						List<StoreManageInfo> datas = gson.fromJson(mallGoodOutDTOList, new TypeToken<List<StoreManageInfo>>() {
//						}.getType());
//
//					}

				} catch (JSONException e) {
					e.printStackTrace();

				}
			}

			@Override
			public void failed(Throwable error) {
				showE404();
				disShowProgress();
			}
		});
	}

	private void cleanCacheData(){
		DataCleanManager.deleteCache(mContext);
		DecimalFormat df = new DecimalFormat("######0.00"); 
		toast("成功清楚缓存"+df.format(currentCacheSize)+"M");
		currentCacheSize = 0.0;
	}
	
}
